import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _class;

/**
 * DataAuditModal - 数据审计弹窗
 * @date: 2019/5/9
 */
import React, { Component } from 'react';
import { Button, Modal } from 'antd';
import { Bind } from 'lodash-decorators';
import { getResponse, createPagination } from "../../utils/utils";
import intl from "../../utils/intl";
import { queryAuditVersion } from "../../services/api";
import VersionList from "./VersionList";
import DetailList from "./DetailList";
/**
 * 数据审计弹窗
 * @extends {Component} - Component
 * @reactProps {!string} entityCode - 审计编码，由后端在列表查询接口中返回
 * @reactProps {!number} entityId - 实体ID，对应列表查询接口中的主键id的值
 * @reactProps {array} columns 页面列配置
 * @return React.element
 */

var DataAuditModal = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), (_class = /*#__PURE__*/function (_Component) {
  _inherits(DataAuditModal, _Component);

  var _super = _createSuper(DataAuditModal);

  function DataAuditModal() {
    var _this;

    _classCallCheck(this, DataAuditModal);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = _this.getInitState();
    return _this;
  }

  _createClass(DataAuditModal, [{
    key: "getInitState",
    value:
    /**
     * 初始化state
     */
    function getInitState() {
      return {
        visible: false,
        versionList: [],
        versionPage: {},
        detailList: [],
        versionLoading: false,
        detailLoading: false
      };
    }
    /**
     * 查询审计版本数据
     */

  }, {
    key: "fetchData",
    value: function fetchData() {
      var _this2 = this;

      var fields = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      this.setState({
        versionLoading: true
      });
      var _this$props = this.props,
          entityCode = _this$props.entityCode,
          entityId = _this$props.entityId;
      var params = {
        page: _isEmpty(fields) ? {} : fields,
        entityCode: entityCode,
        entityId: entityId
      };
      queryAuditVersion(params).then(function (res) {
        if (getResponse(res)) {
          _this2.setState({
            versionList: res.content || [],
            versionPage: createPagination(res),
            versionLoading: false
          });
        } else {
          _this2.setState({
            versionLoading: false
          });
        }
      });
    }
    /**
     * 查询审计版本详情数据
     * @param {number} auditDataId 审计版本数据中的主键
     */

  }, {
    key: "fetchDetailData",
    value: function fetchDetailData(auditDataId) {
      var versionList = this.state.versionList;
      this.setState({
        detailLoading: true,
        detailList: []
      });
      var targetVersion = versionList.find(function (item) {
        return item.auditDataId === auditDataId;
      });
      var auditDataLineList = targetVersion.auditDataLineList;
      this.setState({
        detailList: auditDataLineList,
        detailLoading: false
      });
    }
    /**
     * 显示模态框
     */

  }, {
    key: "handleOpenModal",
    value: function handleOpenModal() {
      this.setState({
        visible: true
      });
      this.fetchData();
    }
    /**
     * 关闭模态框
     */

  }, {
    key: "handleCloseModal",
    value: function handleCloseModal() {
      this.setState(this.getInitState());
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var _this$props$columns = this.props.columns,
          columns = _this$props$columns === void 0 ? [] : _this$props$columns;
      var _this$state = this.state,
          visible = _this$state.visible,
          versionList = _this$state.versionList,
          versionPage = _this$state.versionPage,
          detailList = _this$state.detailList,
          versionLoading = _this$state.versionLoading,
          detailLoading = _this$state.detailLoading;
      var versionListProps = {
        dataSource: versionList,
        pagination: versionPage,
        loading: versionLoading,
        onFetch: this.fetchDetailData,
        onChange: this.fetchData
      };
      var detailListProps = {
        dataSource: detailList,
        comparedData: columns,
        loading: detailLoading
      };
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("a", {
        onClick: function onClick() {
          return _this3.handleOpenModal();
        }
      }, intl.get('cpaas.common.components.dataAudit.viewHistory').d('查看数据历史')), visible && /*#__PURE__*/React.createElement(Modal, {
        width: "50%",
        visible: visible,
        destroyOnClose: true,
        title: intl.get('cpaas.common.components.dataAudit.changeHistory').d('数据变更历史'),
        onCancel: this.handleCloseModal,
        footer: [/*#__PURE__*/React.createElement(Button, {
          key: "cancel",
          onClick: this.handleCloseModal
        }, intl.get('cpaas.common.button.close').d('关闭'))]
      }, /*#__PURE__*/React.createElement("div", {
        style: {
          maxHeight: '60vh',
          overflowY: 'scroll'
        }
      }, /*#__PURE__*/React.createElement(VersionList, versionListProps), /*#__PURE__*/React.createElement(DetailList, detailListProps))));
    }
  }]);

  return DataAuditModal;
}(Component), (_applyDecoratedDescriptor(_class.prototype, "getInitState", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "getInitState"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "fetchData", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "fetchData"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "fetchDetailData", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "fetchDetailData"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleOpenModal", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "handleOpenModal"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleCloseModal", [_dec5], Object.getOwnPropertyDescriptor(_class.prototype, "handleCloseModal"), _class.prototype)), _class));
export { DataAuditModal as default };